package com.top.mall.main.order

import android.os.Bundle
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import com.top.mall.R
import com.top.mall.custom.setTransitionStatus
import com.top.mall.jetpack.vm.OrderViewModel
import com.top.mall.main.income.CouponCashFragment
import com.top.mall.main.order.adapter.OrderGroupAdapter
import kotlinx.android.synthetic.main.fragment_order_group.*

class OrderGroupFragment : OwnerViewModeFragment<OrderViewModel>() {
    //waitpay=待支付 waitsend=待发货 waitreceive=待收货 finish = 已完成
    companion object {
        const val KEY_COLLECT_TYPE = "KEY_COLLECT_TYPE"
        fun newInstance(type: Int): OrderGroupFragment {
            val bundle = Bundle().apply {
                putInt(KEY_COLLECT_TYPE, type)
            }
            return OrderGroupFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mType: String?
        get() {
            val type = arguments?.getInt(KEY_COLLECT_TYPE, 0) ?: 0
            return if (type == 1) "waitpay" else if (type == 2) "waitsend" else if (type == 3) "waitreceive" else if (type == 4) "finish" else null
        }

    private val orderGroupAdapter: OrderGroupAdapter = OrderGroupAdapter()

    override val layoutResId: Int
        get() = R.layout.fragment_order_group

    override fun viewModel(): OrderViewModel {
        return createViewModel()
    }

    override fun initView() {

        //设置头部刷新颜色
        mhOrderGroupRefresh.setColorSchemeResources(R.color.colorMain)

        rvOrderGroupBody.adapter = orderGroupAdapter

        orderGroupAdapter.setOnItemChildClickListener { _, view, position ->

            when (view.id) {
                R.id.rvOrderGroupQrCode -> {
                    //订单码
                }
                R.id.rvOrderGroupReturn -> {
                    //申请退货
                    OrderEvaluateActivity.open(requireActivity())
                }
                R.id.rvOrderGroupDelete -> {
                    //删除订单
                }
                R.id.rvOrderGroupCancel -> {
                    //取消订单
                }
                R.id.rvOrderGroupRevoke -> {
                    //撤销申请
                }
                R.id.rvOrderGroupUpdate -> {
                    //修改申请
                }
                R.id.rvOrderGroupAgain -> {
                    //再来一单
                }
                R.id.rvOrderGroupPayment -> {
                    //去支付
                    OrderPaymentActivity.open(orderGroupAdapter.getItem(position).id)
                }
            }
        }

        orderGroupAdapter.setOnItemClickListener { _, _, position ->
            val itemData = orderGroupAdapter.getItem(position)
            viewModel.orderDetails(itemData.id)
        }

        orderGroupAdapter.loadMoreModule.setOnLoadMoreListener {
            orderGroupAdapter.loadMoreModule.loadMoreEnd()
        }
    }

    override fun initData() {
        viewModel.orderIndex(mType)
    }

    override fun onRefreshLoad() {
        super.onRefreshLoad()
        viewModel.orderIndex(mType)
    }

    override fun initViewMode() {
        viewModel.orderIndexLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data ?: return@observe
            orderGroupAdapter.setNewInstance(data)
        })

        viewModel.orderDetailsLiveData.observe(viewLifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            OrderDetailsActivity.open(data)
        })
    }

}